package edu.gatech.cc.hwa.server.features;

import edu.gatech.cc.hwa.server.Profile;

public class BrowserFeature extends Feature {
	
	public static final int ANY = -1;
	public static final int IE_8 = 0;
	public static final int IE_7 = 1;
	public static final int IE_6 = 2;
	public static final int FIREFOX_4_0 = 3;
	public static final int FIREFOX_3_6 = 4;
	public static final int FIREFOX_3_5 = 5;
	public static final int CHROME_10 = 6;
	public static final int CHROME_9 = 7;
	public static final int SAFARI_5 = 8;
	public static final int OPERA_11 = 9;
	public static final int UNKNOWN = 10;
	
	private final String name;
	private final String version;	
	
	public BrowserFeature(String browserName, String browserVersion){
		super(browserName+":"+browserVersion);
		name = browserName;
		version = browserVersion;
	}

	@Override
	public boolean matchesProfile(Profile profile) {
		int profileBrowser = profile.getBroswer();
		if (profileBrowser == ANY) {return true;}
		return profileBrowser == mapToId();
	}
	
	public int mapToId() {
		return maptoId(name, version);
	}
	
	public static int maptoId(String name, String version) {
		if (name.equals("MSIE")) {
			if (version.equals("8.0")) {
				return IE_8;
			} else if (version.equals("7.0")) {
				return IE_7;
			} else if (version.equals("6.0")) {
				return IE_6;
			} else {
				return UNKNOWN;
			}
		} else if (name.equals("Firefox")) {
			if (version.equals("4")) {
				return FIREFOX_4_0;
			} else if (version.equals("3.6")) {
				return FIREFOX_3_6;
			} else if (version.equals("3.5")) {
				return FIREFOX_3_5;
			} else {
				return UNKNOWN;
			}
		} else if (name.equals("Chrome")) {
			if (version.equals("10")) {
				return CHROME_10;
			} else if (version.equals("9")) {
				return CHROME_9;
			} else {
				return UNKNOWN;
			}
		} else if (name.equals("Safari") & version.equals("5")) {
			return SAFARI_5;
		} else if (name.equals("Opera") & version.equals("9.8")) {
			return OPERA_11;
		} else {
			return UNKNOWN;
		}
	}
}
